<?php
include_once("adminBase.php");
include_once(_INC_PATH.'catecae.php');

class Corporation extends AdminBase{
	function del()
	{
		$cor_id = $_REQUEST['id'];
		$sql = " UPDATE	`tbl_corporation` SET del_flg = '1',delete_time=CURRENT_TIMESTAMP WHERE id ='".$cor_id."'";
		$this->db->query($sql);
		
		$sql = "select id from `tbl_estimate` where cor_id ='".$cor_id."'";
		$this->db->setQuery($sql);
		$est_ids = $this->db->loadResultList('id');
		if(is_array($est_ids) && count($est_ids) > 0)
		{
			$where = " where estimate_id in ('".implode("','",$est_ids)."')";
			$sql = "update `tbl_response` set del_flg = '1',delete_time=CURRENT_TIMESTAMP $where";
			$this->db->query($sql);
		}
					
		//删除该企业的评价
		$sql = "update `tbl_estimate` set del_flg = '1',delete_time=CURRENT_TIMESTAMP where cor_id ='".$cor_id."'";
		$this->db->query($sql);
		
		$this->managerLog($cor_id, 'tbl_corporation');
		
		if($_REQUEST['method'] == "del"){
			echo "ok";die;
		}
	}
	
	function delAll()
	{
		if(is_array($_REQUEST['selectId']) && count($_REQUEST['selectId']) > 0)
		{
			foreach($_REQUEST['selectId'] as $key=>$value)
			{
				$id = intval($value);
				if($id > 0){
					$_REQUEST['id'] = $id;
					$this->del();
				}
			}
		}
		$this->index();
	}
	
	function index()
	{
		global $cateSubCae;
		$where = ' and 1=1'.($_SESSION['admin_login']['grade'] > 1 ? ' and (length(ccid)=0 or ccid=\'0\')' : '');
		if($_POST['cor_id']) $where .= " and tc.id='".$_POST['cor_id']."'";
		if($_POST['cor_name']) $where .= " and tc.name like '%".$_POST['cor_name']."%'";
		if($_POST['user_id']) $where .= " and tc.create_author='".$_POST['user_id']."'";
		if($_POST['user_name']) $where .= " and tu.user_name like '%".$_POST['user_name']."%'";
		if($_POST['time_1']) $where .= " and left(tc.create_time,10)>='".$_POST['time_1']."'";
		if($_POST['time_2']) $where .= " and left(tc.create_time,10)<='".$_POST['time_2']."'";
		$sql="SELECT count(*)as total FROM `tbl_corporation` tc where del_flg='0' $where";
		$total = $this->db->loadResult($sql);
		$curPage = @intval($_REQUEST['p']);
		if($curPage < 1) $curPage = 1;
		$showNum = _BACKVIEWNUM;
		$pageLink = "/management/admin_corporation.php";
		$page = new PageClass($total,$curPage,$showNum,$pageLink);
		$limitStart = $page->limitStart;
		$this->smarty->assign("pageList",$page->pageList);
		
		$sql = "SELECT tc.* FROM `tbl_corporation` tc where del_flg='0' ".$where." order by tc.create_time desc limit $limitStart,$showNum";
		$this->db->setQuery($sql);
		$data = $this->db->loadList();
		foreach($data as $key=>$value)
		{
			if($value['ccid'] != ""){
				$subcate = "";
				$temp = explode(',', $value['ccid']);
				foreach($temp as $kk=>$vv){
					if($vv != ""){
						if(array_key_exists($vv,$cateSubCae)){
							$subcate .= $cateSubCae[$vv]." | ";
						}
					}
				}
				$data[$key]['ccname'] = substr($subcate,0,-3);
			}
		}
		$this->smarty->assign("row",$data);
		$this->smarty->assign("cateSubCae",$cateSubCae);
		$this->smarty->assign("curPage",$curPage);
		adminDisplay("admin_corporation.tpl");
	}
	
	function delCompletely()
	{
		if(is_array($_REQUEST['selectId']) && count($_REQUEST['selectId']) > 0)
		{
			foreach($_REQUEST['selectId'] as $key=>$value)
			{
				$id = intval($value);
				if($id > 0)
				{
					$sql = "delete from `tbl_corporation` where id='".$id."'";
					$this->db->query($sql);
					//删除该企业的评价
					$sql = "select id from `tbl_estimate` where cor_id ='".$id."'";
					$this->db->setQuery($sql);
					$est_ids = $this->db->loadResultList('id');
					if(is_array($est_ids) && count($est_ids) > 0)
					{
						$where = " where estimate_id in ('".implode("','",$est_ids)."')";
						$sql = "delete from `tbl_response` $where";
						$this->db->query($sql);
					}
					$sql = "delete from `tbl_estimate` where cor_id ='".$id."'";
					$this->db->query($sql);
				}
			}
		}
		$this->index();
	}
	
	function changeCate()
	{
		global $cateSubCae;;
		$ccids = $_POST['ccids'];
		$id = intval($_POST['id']);
		$upateSql = "update `tbl_corporation` set ccid='".$ccids."' where id='".$id."'";
		$this->db->query($upateSql);
		$arr = explode(",",$ccids);
		$returnStr = "";
		foreach($arr as $key=>$value){
			if(array_key_exists($value,$cateSubCae) && $value != ""){
				$returnStr .= $cateSubCae[$value]." | ";
			}
		}
		$this->managerLog($id, 'tbl_corporation');
		echo $id."@@".substr($returnStr,0,-3);
		die;
	}
}
$corporation = new Corporation();